home *** CD-ROM | disk | FTP | other *** search
/ isnet Internet / Isnet Internet CD.iso / prog / hiz / 09 / 09.exe / adynware.exe / perl / lib / site / Tk.pm < prev    next >
Encoding:
Perl POD Document  |  1999-12-28  |  8.7 KB  |  472 lines

  1. package Tk;
  2. require 5.004;
  3. use     AutoLoader qw(AUTOLOAD);
  4. use     DynaLoader;
  5. require Exporter;
  6. @Tk::ISA = qw(Exporter DynaLoader);
  7.  
  8.  
  9. @EXPORT    = qw(Exists Ev after exit MainLoop DoOneEvent tkinit);
  10. @EXPORT_OK = qw(NoOp *widget *event lsearch catch 
  11.                 DONT_WAIT WINDOW_EVENTS  FILE_EVENTS TIMER_EVENTS 
  12.                 IDLE_EVENTS ALL_EVENTS 
  13.                 NORMAL_BG ACTIVE_BG SELECT_BG 
  14.                 SELECT_FG TROUGH INDICATOR DISABLED BLACK WHITE);
  15. %EXPORT_TAGS = (eventtypes => [qw(DONT_WAIT WINDOW_EVENTS  FILE_EVENTS 
  16.                                   TIMER_EVENTS IDLE_EVENTS ALL_EVENTS)], 
  17.                 variables  => [qw(*widget *event)],
  18.                 colors     => [qw(NORMAL_BG ACTIVE_BG SELECT_BG SELECT_FG 
  19.                                   TROUGH INDICATOR DISABLED BLACK WHITE)],
  20.                );
  21.  
  22. use strict;
  23. use Symbol ();
  24.  
  25. use Carp;
  26.  
  27. $Tk::version     = "4.2";
  28. $Tk::patchLevel  = "4.2";
  29. $Tk::VERSION     = '402.002';
  30. $Tk::strictMotif = 0;
  31.                                    
  32. {($Tk::library) = __FILE__ =~ /^(.*)\.pm$/;}
  33. $Tk::library = Tk->findINC('.') unless (-d $Tk::library);
  34.  
  35. $Tk::widget  = undef;
  36. $Tk::event   = undef;
  37.  
  38. use vars qw($TkVtab $TkintVtab $LangVtab $TkglueVtab $XlibVtab $TkoptionVtab);  
  39. use vars qw($TixVtab $TixintVtab $TiximgxpmVtab);
  40.  
  41. bootstrap Tk $Tk::VERSION;
  42.  
  43. {
  44.  no strict 'refs';
  45.  *{'exit'} = \&Exit;
  46. }
  47. my $boot_time = timeofday();
  48.  
  49. Preload(DynaLoader::dl_findfile('-L/usr/openwin/lib','-lX11')) if (&NeedPreload && -d '/usr/openwin/lib');
  50.  
  51. use Tk::Submethods ('option'    =>  [qw(add get clear readfile)],
  52.                     'clipboard' =>  [qw(clear append)]
  53.                    );
  54.  
  55. sub BackTrace
  56. {
  57.  my $w = shift;
  58.  return unless (@_ || $@);
  59.  my $mess = (@_) ? shift : "$@";
  60.  my $i = 0;  
  61.  my ($pack,$file,$line,$sub) = caller($i++);
  62.  while (1)   
  63.   {          
  64.    my $loc = "at $file line $line";
  65.    ($pack,$file,$line,$sub) = caller($i++);
  66.    last if (!defined($sub) || $sub eq '(eval)');
  67.    $w->AddErrorInfo("$sub $loc");
  68.   }          
  69.  die "$mess\n";
  70. }
  71.  
  72. sub NoOp  { }
  73.  
  74. sub Ev
  75. {
  76.  my @args = @_;
  77.  my $obj;
  78.  if (@args == 1)
  79.   {
  80.    my $arg = pop(@args);
  81.    $obj = (ref $arg) ? $arg : \$arg;
  82.   }
  83.  else 
  84.   {
  85.    $obj = \@args;
  86.   }
  87.  return bless $obj,"Tk::Ev";
  88. }
  89.  
  90. sub InitClass
  91. {
  92.  my ($package,$parent) = @_;
  93.  croak "Unexpected type of parent $parent" unless(ref $parent);
  94.  croak "$parent is not a widget" unless($parent->IsWidget);
  95.  my $mw = $parent->MainWindow;
  96.  unless (exists $mw->{'_ClassInit_'}{$package})
  97.   {
  98.    $package->Install($mw);
  99.    $mw->{'_ClassInit_'}{$package} = $package->ClassInit($mw);
  100.   }
  101. }
  102.  
  103. require Tk::Widget;
  104. require Tk::Image;
  105. require Tk::MainWindow;
  106.  
  107. sub Exists
  108. {my $w = shift;
  109.  return defined($w) && ref($w) && $w->IsWidget && $w->exists;
  110. }
  111.  
  112. sub Time_So_Far
  113. {
  114.  return timeofday() - $boot_time;
  115.  
  116.  
  117. sub SelectionOwn
  118. {my $widget = shift;
  119.  selection('own',(@_,$widget));
  120. }
  121.  
  122. sub SelectionOwner
  123. {
  124.  selection('own',"-displayof",@_);
  125. }
  126.  
  127. sub SelectionClear
  128. {
  129.  selection('clear',"-displayof",@_);
  130. }
  131.  
  132. sub SelectionExists
  133. {
  134.  selection('exists',"-displayof",@_);
  135. }
  136.  
  137. sub SelectionHandle
  138. {my $widget = shift;
  139.  my $command = pop;
  140.  selection('handle',@_,$widget,$command);
  141. }
  142.  
  143. sub __DIE__
  144. {
  145.  my $mess = shift;
  146.  my $w = $Tk::widget;
  147.  if (defined $w)
  148.   {
  149.    my $i = 0;  
  150.    my ($pack,$file,$line,$sub) = caller($i++);
  151.    while (1)   
  152.     {          
  153.      my $loc = "at $file line $line";
  154.      ($pack,$file,$line,$sub) = caller($i++);
  155.      last if (!defined($sub) || $sub eq '(eval)');
  156.      $w->AddErrorInfo("$sub $loc");
  157.     }          
  158.   }
  159. }
  160.  
  161. sub fileevent
  162. {
  163.  require Tk::IO;
  164.  my ($obj,$file,$mode,$cb) = @_;
  165.  croak "Unknown mode '$mode'" unless $mode =~ /^(readable|writeable)$/;
  166.  unless (ref $file)
  167.   {
  168.    require IO::Handle;
  169.    no strict 'refs';
  170.    $file = Symbol::qualify($file,(caller)[0]);
  171.    $file = bless \*{$file},'IO::Handle';
  172.   }
  173.  if ($cb)
  174.   {
  175.    $cb = Tk::Callback->new($cb);
  176.    if ($mode eq 'readable')
  177.     {
  178.      Tk::IO::CreateReadHandler($file,$cb);
  179.     }
  180.    else
  181.     {
  182.      Tk::IO::CreateWriteHandler($file,$cb);
  183.     }
  184.   }
  185.  else
  186.   {
  187.    if ($mode eq 'readable')
  188.     {
  189.      Tk::IO::DeleteReadHandler($file);
  190.     }
  191.    else
  192.     {
  193.      Tk::IO::DeleteWriteHandler($file);
  194.     }
  195.   }
  196. }
  197.  
  198. sub SplitString
  199. {
  200.  local $_ = shift;
  201.  carp "SplitString '$_'";
  202.  my (@arr, $tmp);
  203.  while (/\{([^{}]*)\}|((?:[^\s\\]|\\.)+)/gs) {
  204.    if (defined $1) { push @arr, $1 }
  205.    else { $tmp = $2 ; $tmp =~ s/\\([\s\\])/$1/g; push @arr, $tmp }
  206.  }
  207.  return @arr;
  208. }
  209.  
  210.  
  211. 1;
  212.  
  213. __END__
  214. sub exit { CORE::exit(@_);}
  215.  
  216. sub Exists
  217. {my $w = shift;
  218.  return defined($w) && ref($w) && $w->IsWidget && $w->exists;
  219. }
  220.  
  221. sub Error
  222. {my $w = shift;
  223.  my $error = shift;
  224.  if (Exists($w))
  225.   {
  226.    my $grab = $w->grab('current');  
  227.    $grab->Unbusy if (defined $grab);
  228.   }
  229.  chomp($error);
  230.  warn "Tk::Error: $error\n " . join("\n ",@_);
  231. }
  232.  
  233. sub tkinit
  234. {
  235.  return MainWindow->new(@_);
  236. }
  237.  
  238. sub CancelRepeat
  239. {
  240.  my $w = shift->MainWindow;
  241.  my $id = delete $w->{_afterId_};
  242.  $w->after('cancel',$id) if (defined $id);
  243. }
  244.  
  245. sub RepeatId
  246. {
  247.  my ($w,$id) = @_;
  248.  $w = $w->MainWindow;
  249.  $w->CancelRepeat;
  250.  $w->{_afterId_} = $id;
  251. }
  252.  
  253.  
  254.  
  255.  
  256. sub FocusChildren { shift->children }
  257.  
  258. sub focusNext
  259. {
  260.  my $w = shift;
  261.  my $cur = $w;
  262.  while (1)
  263.   {
  264.    my $parent = $cur;
  265.    my @children = $cur->FocusChildren();
  266.    my $i = -1;
  267.    while (1)
  268.     {
  269.      $i += 1;
  270.      if ($i < @children)
  271.       {
  272.        $cur = $children[$i];
  273.        next if ($cur->toplevel == $cur);
  274.        last
  275.       }
  276.      $cur = $parent;
  277.      last if ($cur->toplevel() == $cur);
  278.      $parent = $parent->parent();
  279.      @children = $parent->FocusChildren();
  280.      $i = lsearch(\@children,$cur);
  281.     }
  282.    if ($cur == $w || $cur->FocusOK)
  283.     {
  284.      $cur->Tk::focus;
  285.      return;
  286.     }
  287.   }
  288. }
  289. sub focusPrev
  290. {
  291.  my $w = shift;
  292.  my $cur = $w;
  293.  my @children;
  294.  my $i;
  295.  my $parent;
  296.  while (1)
  297.   {
  298.    if ($cur->toplevel() == $cur)
  299.     {
  300.      $parent = $cur;
  301.      @children = $cur->FocusChildren();
  302.      $i = @children;
  303.     }
  304.    else
  305.     {
  306.      $parent = $cur->parent();
  307.      @children = $parent->FocusChildren();
  308.      $i = lsearch(\@children,$cur);
  309.     }
  310.    while ($i > 0)
  311.     {
  312.      $i--;
  313.      $cur = $children[$i];
  314.      next if ($cur->toplevel() == $cur);
  315.      $parent = $cur;
  316.      @children = $parent->FocusChildren();
  317.      $i = @children;
  318.     }
  319.    $cur = $parent;
  320.    if ($cur == $w || $cur->FocusOK)
  321.     {
  322.      $cur->Tk::focus;
  323.      return;
  324.     }
  325.   }
  326.  
  327. }
  328.  
  329. sub FocusOK
  330. {
  331.  my $w = shift;
  332.  my $value;
  333.  catch { $value = $w->cget('-takefocus') };
  334.  if (!$@ && defined($value))
  335.   {
  336.    return 0 if ($value eq '0');
  337.    return 1 if ($value eq '1');
  338.    $value = $w->$value();
  339.    return $value if (defined $value);
  340.   }
  341.  if (!$w->viewable)
  342.   {
  343.    return 0;
  344.   }
  345.  catch { $value = $w->cget('-state') } ;
  346.  if (!$@ && defined($value) && $value eq "disabled")
  347.   {
  348.    return 0;
  349.   }
  350.  $value = grep(/Key|Focus/,$w->Tk::bind(),$w->Tk::bind(ref($w)));
  351.  return $value;
  352. }
  353.  
  354.  
  355.  
  356. sub EnterFocus
  357. {
  358.  my $w  = shift;
  359.  my $Ev = $w->XEvent;
  360.  my $d  = $Ev->d;
  361.  $w->Tk::focus() if ($d eq "NotifyAncestor" ||  $d eq "NotifyNonlinear" ||  $d eq "NotifyInferior");
  362. }
  363.  
  364. sub focusFollowsMouse
  365. {
  366.  my $widget = shift;
  367.  $widget->bind('all',"EnterFocus");
  368. }
  369.  
  370. sub TraverseToMenu
  371. {
  372.  my $w = shift;
  373.  my $char = shift;
  374.  return unless(defined $char && $char ne "");
  375.  $w = $w->toplevel->FindMenu($char);
  376.  $w->PostFirst() if (defined $w);
  377. }
  378. sub FirstMenu
  379. {
  380.  my $w = shift;
  381.  $w = $w->toplevel->FindMenu("");
  382.  $w->PostFirst() if (defined $w);
  383. }
  384.  
  385.  
  386. sub Selection
  387. {my $widget = shift;
  388.  my $cmd    = shift;
  389.  croak "Use SelectionOwn/SelectionOwner" if ($cmd eq 'own');
  390.  croak "Use Selection\u$cmd()";
  391. }
  392.  
  393. sub Clipboard
  394. {my $w = shift;
  395.  my $cmd    = shift;
  396.  croak "Use clipboard\u$cmd()";
  397. }
  398.  
  399. sub Receive
  400. {
  401.  my $w = shift;
  402.  warn "Receive(" . join(',',@_) .")";
  403.  die "Tk rejects send(" . join(',',@_) .")\n";
  404. }
  405.  
  406. sub break
  407. {
  408.  die "_TK_BREAK_\n";
  409. }
  410.  
  411. sub idletasks
  412. {
  413.  shift->update('idletasks');
  414. }
  415.  
  416. sub updateWidgets
  417. {
  418.  my ($w) = @_;
  419.  while ($w->DoOneEvent(DONT_WAIT|IDLE_EVENTS|WINDOW_EVENTS))
  420.   {
  421.   }
  422.  $w;
  423. }
  424.  
  425. sub ImageNames
  426. {
  427.  image('names');
  428. }
  429.  
  430. sub ImageTypes
  431. {
  432.  image('types');
  433. }
  434.  
  435. sub interps
  436. {
  437.  my $w = shift;
  438.  return $w->winfo('interps','-displayof');
  439. }
  440.  
  441. sub findINC
  442. {
  443.  my $file = join('/',@_);
  444.  my $dir;
  445.  $file  =~ s,::,/,g;
  446.  foreach $dir (@INC)
  447.   {
  448.    my $path;
  449.    return $path if (-e ($path = "$dir/$file"));
  450.   }
  451.  return undef;
  452. }
  453.  
  454. sub lsearch
  455. {my $ar = shift;
  456.  my $x  = shift;
  457.  my $i;
  458.  for ($i = 0; $i < scalar @$ar; $i++)
  459.   {
  460.    return $i if ($$ar[$i] eq $x);
  461.   }
  462.  return -1;
  463. }
  464.  
  465. sub catch (&)
  466. {
  467.  my $sub = shift;
  468.  eval {local $SIG{'__DIE__'}; &$sub };
  469. }
  470.  
  471.